<template>
  <view class="container screen-width">
    <view class="tab_title">
      <image src="../../static/returnimg.png" mode @tap="backPage" />
      <text>{{ titleText }}</text>
    </view>
    <VBar :datatab="tabList" @tab="handleChange" />
    <scroll-view class="content" :scroll-y="true" @scrolltolower="scrolltolower">
      <view v-if="current === 0" class="policy-list">
        <view
          class="flex items-middle space-between policy-item"
          v-for="(item,index) in policyList"
          :key="index"
          @click="toPolicyDetail(item)"
        >
          <view class="name">{{ item.title }}</view>
          <view class="date">{{ item.date }}</view>
        </view>
        <van-divider v-show="!policyList.length">暂无数据</van-divider>
        <van-divider v-show="policyList.length +1 > total">我也是有底线的</van-divider>
      </view>
      <view v-else class="policy-list">
        <view class="policy-item" v-for="(item,index) in publicList" :key="index">
          <view class="flex items-middle space-between">
            <view class="name">{{ titleText }}公示名单</view>
            <view class="date">{{ item.releaseDate }}</view>
          </view>
          <view class="link flex">
            <view v-if="item.policyId" @click="toPolicy(item.policyId)">名单来源</view>
            <a v-if="!item.policyId && item.originalLink" :href="item.originalLink">名单来源</a>
            <view @click="toPublickList(item)" style="margin-left: 60upx">相关企业</view>
          </view>
        </view>
        <van-divider v-show="!publicList.length">暂无数据</van-divider>
        <van-divider v-show="publicList.length+1 > total">我也是有底线的</van-divider>
      </view>
    </scroll-view>
    <Festival></Festival>
  </view>
</template>
<script>
import { policyList } from "../../util/api/es.js";
import { getProjectBatchNumber } from "../../util/api/policy.js";
import { getCode } from "../../util/address.js";
export default {
  data() {
    return {
      tabList: [{ name: "相关政策" }, { name: "公示名单" }],
      current: 0,
      titleText: "", //查询政策title
      policyList: [],
      publicList: [],
      throttle: false,
      pageNo: 1,
      total: 0,
      province: "",
      city: "",
      companyId: "",
      years: ''
    };
  },
  onLoad(e) {
    this.titleText = e.title;
    this.province = e.province;
    this.city = e.city;
    this.companyId = e.id;
    this.years = e.years
    this.getPolicyList();
  },
  methods: {
    backPage() {
      uni.navigateBack({
        // 点击返回到上一页
        delta: 1
      });
    },
    // tab切换
    handleChange(index) {
      this.current = index;
      this.pageNo = 1;
      if (this.current === 0) {
        this.getPolicyList();
      } else {
        this.getPublicList();
      }
    },
    getPolicyList() {
      this.throttle = true;
      uni.showLoading({
        title: "加载中...",
        mask: false
      });
      let area = "";
      let params = {
        beginDate: "",
        dq: getCode(this.province, this.city, area),
        endDate: "",
        pageIndex: this.pageNo - 1,
        pageSize: 15,
        policyId: "",
        source: "",
        title: this.titleText,
        tzlb: "通知",
        userID: ""
      };
      policyList(params)
        .then(res => {
          if (res.retCode === 0) {
            if (this.pageNo - 1 === 0) {
              this.policyList = res.data.content || [];
            } else {
              this.policyList = this.policyList.concat(res.data.content);
            }
            this.total = res.data.totalElements;
          }
        })
        .finally(() => {
          uni.hideLoading();
          this.throttle = false;
        });
    },
    getPublicList() {
      this.throttle = true;
      uni.showLoading({
        title: "加载中...",
        mask: false
      });
      let params = {
        name: this.titleText,
        pageNo: this.pageNo,
        pageSize: 10,
        companyId: this.companyId,
        years: this.years
      };
      getProjectBatchNumber(params)
        .then(res => {
          if (res.code === 200) {
            if (this.pageNo === 1) {
              this.publicList = res.result.records || [];
            } else {
              this.publicList = this.publicList.concat(res.result.records);
            }
            this.total = res.result.total;
          }
        })
        .finally(() => {
          uni.hideLoading();
          this.throttle = false;
        });
    },
    // 政策详情
    toPolicyDetail(item) {
      uni.navigateTo({
        url: `/pages/policy/policyDetails?id=${item.policyId}&inform=通知`
      });
    },
    // 企业公示名单列表
    toPublickList(item) {
      uni.navigateTo({
        url: `/pages/enterpriseDetails/publicList?num=${item.batchNo}&name=${this.titleText}`
      });
    },
    // 政策详情
    toPolicy(id) {
      uni.navigateTo({
        url: `/pages/policy/policyDetails?id=${id}&inform=通知`
      });
    },
    scrolltolower() {
      if (!this.throttle) {
        if (this.current === 0 && this.policyList.length + 1 > this.total) {
          return;
        } else if (
          this.current === 1 &&
          this.publicList.length + 1 > this.total
        ) {
          return;
        }
        this.pageNo = this.pageNo + 1;
        if (this.current === 0) {
          this.getPolicyList();
        } else {
          this.getPublicList();
        }
      }
    }
  }
};
</script>
<style lang="less" scoped>
.container {
  width: 100%;
  height: 100vh;
  .tab_title {
    padding-left: 30rpx;
    height: 90rpx;
    display: flex;
    align-items: center;
    background-color: #2862ad;
    image {
      width: 18rpx;
      height: 34rpx;
      margin-left: 10rpx;
    }
    text {
      color: #fff;
      font-size: 34rpx;
      margin-left: 40rpx;
    }
  }
  .content {
    padding: 20upx 30upx;
    box-sizing: border-box;
    width: 100%;
    height: calc(~"100% - 180upx");
    .policy-list {
      width: 100%;
      .policy-item {
        background: #fff;
        padding: 30upx;
        box-sizing: border-box;
        margin-bottom: 20upx;
        .name {
          width: 75%;
        }
        .date {
          color: #ccc;
        }
        .link {
          color: #1678ff;
          margin-top: 20upx;
        }
      }
    }
  }
}
</style>